﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Text;

namespace SGRapidForm.Common
{
    public class JSManager
    {
        // 用来代替 AjaxPro.Utility.RegisterTypeForAjax(Type type);方法
        public static void RegAjax(HtmlHead Header, string typeName, string assemblyName)
        {
            RegBasicJS(Header);
            Literal ltl = new Literal();
            ltl.Text = string.Format("<script type=\"text/javascript\" src=\"/ajaxpro/{0},{1}.ashx\"></script>\n", typeName, assemblyName);
            Header.Controls.Add(ltl);
        }

        // 用来代替 AjaxPro.Utility.RegisterTypeForAjax(Type type);方法
        public static void RegAjax(HtmlHead Header, Type type)
        {
            RegBasicJS(Header);
            string assemblyName = type.FullName + "," + type.Assembly.FullName.Substring(0, type.Assembly.FullName.IndexOf(","));
            if (type.Assembly.FullName.StartsWith("App_Code."))
                assemblyName = type.FullName + ",App_Code";
            Literal ltl = new Literal();
            ltl.Text = string.Format("<script type=\"text/javascript\" src=\"/ajaxpro/{0}.ashx\"></script>\n", assemblyName);
            Header.Controls.Add(ltl);
        }

        protected static void RegBasicJS(HtmlHead Header)
        {
            Control c = Header.FindControl("ltlBasic");
            if (c == null)
            {
                StringBuilder strHTML = new StringBuilder();
                strHTML.AppendLine("<script type=\"text/javascript\" src=\"/ajaxpro/prototype.ashx\"></script>");
                strHTML.AppendLine("<script type=\"text/javascript\" src=\"/ajaxpro/core.ashx\"></script>");
                strHTML.AppendLine("<script type=\"text/javascript\" src=\"/ajaxpro/converter.ashx\"></script>");
                Literal ltlBasic = new Literal();
                ltlBasic.Text = strHTML.ToString();
                ltlBasic.ID = "ltlBasic";
                Header.Controls.Add(ltlBasic);
            }
        }
    }
}
